import {
  Controller,
  Get,
  Post,
  Body,
  Req,
  Patch,
  Param,
  Delete,
} from '@nestjs/common';
import { SceneListService } from './scene-list.service';
import { CreateSceneListDto } from './dto/create-scene-list.dto';
import { UpdateSceneListDto } from './dto/update-scene-list.dto';
import { Request } from 'express';
import { Public } from 'src/common/decorator/public.decorator';
import { UpdateGroupToScenceListDto } from './dto/update-grouptoscene.dto';

@Controller('scene-list')
export class SceneListController {
  constructor(private readonly sceneListService: SceneListService) {}

  @Post('createScene')
  create(
    @Body() createSceneListDto: CreateSceneListDto,
    @Req() request: Request,
  ) {
    const user = request.user;
    createSceneListDto.creator = (user as any).email;
    return this.sceneListService.create(createSceneListDto);
  }

  @Get('')
  findAll() {
    return this.sceneListService.findAll();
  }

  @Public()
  @Get('getSceneItemById/:id')
  findOne(@Param('id') id: string) {
    return this.sceneListService.findOne(id);
  }

  @Public()
  @Get('checkScenceById/:id')
  checkScenceById(@Param('id') id: string) {
    return this.sceneListService.checkScenceById(id);
  }

  @Get('checkScenceBelongUser/:id')
  checkScenceBelongUser(@Param('id') id: string, @Req() request: Request) {
    const user = request.user;
    return this.sceneListService.checkScenceBelongUser(id, (user as any).email);
  }

  @Get('checkScenceBelongGroup')
  checkScenceBelongGroup(@Body() jsonData: any) {
    return this.sceneListService.checkScenceBelongGroup(jsonData);
  }

  @Get('getAllSceneItemByUser')
  getAllSceneItemByUser(@Req() request: Request) {
    const user = request.user;
    const email = (user as any).email;
    return this.sceneListService.getAllSceneItemByUser(email);
  }
  @Patch(':id')
  update(
    @Param('id') id: string,
    @Body() updateSceneListDto: UpdateSceneListDto,
  ) {
    return this.sceneListService.update(id, updateSceneListDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.sceneListService.remove(id);
  }

  @Get('isScenceItemExist/:scenceId')
  isScenceItemExist(@Param('scenceId') scenceId: string) {
    return this.sceneListService.isScenceItemExist(scenceId);
  }

  // 创建 小组和场景之间的关系
  @Post('createSceneItemToGroup/:groupuuid')
  createSceneItemToGroup(
    @Body() createSceneListDto: CreateSceneListDto,
    @Req() request: Request,
    @Param('groupuuid') groupuuid: string,
  ) {
    const user = request.user;
    createSceneListDto.creator = (user as any).email;
    return this.sceneListService.createSceneItemToGroup(
      createSceneListDto,
      groupuuid,
    );
  }

  @Get('getGroupToScenceItem/:scenceId')
  getGroupToScenceItem(@Param('scenceId') scenceId: string){
    return this.sceneListService.getGroupToScenceItem(scenceId)
  }

  // navigator.sendBeacon 无法携带 token jwt 验证
  @Public()
  @Post('updateSceneItemToGroup/:scenceId')
  updateSceneItemToGroup(@Param('scenceId') scenceId: string,@Body() updateGroupToScenceListDto: UpdateGroupToScenceListDto,){
    return this.sceneListService.updateSceneItemToGroup(scenceId,updateGroupToScenceListDto)
  }

  @Get('unlockAllScences')
  unlockAllScences(@Req() request: Request){
    const user = request.user;
    return this.sceneListService.unlockAllScences((user as any).email)
  }

  @Get('getScenceItemByGroupuuid/:groupuuid')
  getScenceItemByGroupuuid(@Param('groupuuid') groupuuid: string) {
    return this.sceneListService.getScenceItemByGroupuuid(groupuuid);
  }

  @Delete('deleteScenceItemFromGroup/:scenceToGroupId')
  deleteScenceItemFromGroup(@Param('scenceToGroupId') scenceToGroupId: string) {
    return this.sceneListService.deleteScenceItemFromGroup(scenceToGroupId);
  }

  @Get('getAllPublicSceneItem')
  getAllPublicSceneItem() {
    return this.sceneListService.getAllPublicSceneItem();
  }
}
